<?php
	defined('_JEXEC') or die('Restricted access');
	$db = &JFactory::getDBO();
	$session =& JFactory::getSession();
	$moneyToTal = 0;
	for ($i=1; $i<=$session->get('itemAmount'); $i++) {
		$moneyToTal += ($session->get('price'.$i)*$session->get('quantity'.$i));
	}
?>
<script language="javascript">
jQuery(document).ready(function(){
	//Open the shopcart
	$('.table-order').live("click",function(){
		var itemId = $(this).prev('input').val();
		var itemQuantity = $(this).prev('input').prev('input').val();
		
		jQuery.ajax({
			type: 'POST',
			url: ('index.php?option=com_ecommerce&task=addCart'),
			data: {id: itemId, quantity: itemQuantity},
			success: function(result){
				if(result){
					
					jQuery.ajax({
						url: ('index.php?option=com_ecommerce&task=loadLittleCart'),
						success: function(resultHtml){
							if(resultHtml){
								$('#nav-sc-l').html(resultHtml);
								
								jQuery.ajax({
									url: ('index.php?option=com_ecommerce&task=loadCart'),
									success: function(resultHtml){
										if(resultHtml){
											$('#overview').html(resultHtml);
											
											$parent = $(this).parent('.table-col-7').parent('.table-row');
											if(!$parent.hasClass('sold-out')){
												$parent.addClass('item-added');
												$('.shopcart-list').slideDown();
												$('#scrollbar1').tinyscrollbar();
											}
										} else {
											alert('Kan ikke indlæse indkøbsvogn');
										}
									}
								});
								
								
							} else {
								alert('Kan ikke indlæse indkøbsvogn');
							}
						}
					});
					
				} else {
					alert('Kan ikke tilføje til indkøbsvogn');
				}
			}
		});
		return false;
	})
	
	//Delete each cart
	$('.eachClose').live("click",function(){
		var itemId = $(this).prev('input').val();

		jQuery.ajax({
			type: 'POST',
			url: ('index.php?option=com_ecommerce&task=deleteItem'),
			data: {id: itemId},
			success: function(result){
				if(result){
					
					jQuery.ajax({
						url: ('index.php?option=com_ecommerce&task=loadLittleCart'),
						success: function(resultHtml){
							if(resultHtml){
								$('#nav-sc-l').html(resultHtml);
								
								jQuery.ajax({
									url: ('index.php?option=com_ecommerce&task=loadCart'),
									success: function(resultHtml){
										
										$('#overview').html(resultHtml);
										
										$parent = $(this).parent('.eachCart');
										$parent.slideUp('fast',function(){
											updateScrollbar();
										})
										
										$parent = $('#id'+itemId).parent('.table-col-7').parent('.table-row');
										$parent.removeClass('item-added');
										// If there's only ONE item, them close the shopcart
										/*if($('.eachCart').length == 1)
											$('.shopcart-list').slideUp();*/
											
									}
								});
								
								
							} else {
								alert('Kan ikke indlæse indkøbsvogn 11');
							}
						}
					});
					
									
				} else {
					alert('Kan ikke slette denne post');
				}
			}
		});
		return false;
	})
	
	$('.btnRefresh').live("click",function(){
		
		var itemId = $(this).prev('input').val();
		var itemQuantity = $(this).siblings('input.inputSoluong').val();
		
		jQuery.ajax({
			type: 'POST',
			url: ('index.php?option=com_ecommerce&task=updateCart'),
			data: {id: itemId, quantity: itemQuantity},
			success: function(result){
				if(result){
					
					jQuery.ajax({
						url: ('index.php?option=com_ecommerce&task=loadLittleCart'),
						success: function(resultHtml){
							if(resultHtml){
								$('#nav-sc-l').html(resultHtml);
								
								jQuery.ajax({
									url: ('index.php?option=com_ecommerce&task=loadCart'),
									success: function(resultHtml){
										if(resultHtml){
											$('#overview').html(resultHtml);
										} else {
											alert('Kan ikke indlæse indkøbsvogn');
										}
									}
								});
								
								
							} else {
								alert('Kan ikke indlæse indkøbsvogn');
							}
						}
					});
					
				} else {
					alert('Kan ikke tilføje til indkøbsvogn');
				}
			}
		});
	})
	
	$('#comment').blur(function() {
  		$('#btnCheckout').attr('href','index.php?option=com_ecommerce&view=overall&layout=confirm&Itemid=18&comment='+$('#comment').val());
	})
	
	$('#btnReorder').live("click",function() {
		var lstInput = $(this).siblings('ul.table').find('input.styled:checked');  
  		
		var arrId = new Array();
		var arrQuantity = new Array();
		var i = 0;
		$(lstInput).each(function(){
			arrId[i] = $(this).prev().prev().val();
			arrQuantity[i] = $(this).prev().prev().prev().val();
			i++;
		})
		
		jQuery.ajax({
			type: 'POST',
			url: ('index.php?option=com_ecommerce&task=addOrderCart'),
			data: {id: arrId, quantity: arrQuantity},
			success: function(result){
				if(result){
					
					jQuery.ajax({
						url: ('index.php?option=com_ecommerce&task=loadLittleCart'),
						success: function(resultHtml){
							if(resultHtml){
								$('#nav-sc-l').html(resultHtml);
								
								jQuery.ajax({
									url: ('index.php?option=com_ecommerce&task=loadCart'),
									success: function(resultHtml){
										if(resultHtml){
											$('#overview').html(resultHtml);
											
											$parent = $(this).parent('.table-col-7').parent('.table-row');
											if(!$parent.hasClass('sold-out')){
												$parent.addClass('item-added');
												$('.shopcart-list').slideDown();
												$('#scrollbar1').tinyscrollbar();
											}
										} else {
											alert('Kan ikke indlæse indkøbsvogn');
										}
									}
								});
								
								
							} else {
								alert('Kan ikke indlæse indkøbsvogn');
							}
						}
					});
					
				} else {
					alert('Kan ikke tilføje til indkøbsvogn');
				}
			}
		});
		return false;
	})
});
</script>
<!--Navigation begin-->
<div class="navigation clear-fix">
    <ul>
        <li>					
            <a href="index.php?option=com_ecommerce&view=list&Itemid=16" <?php if(JRequest::getVar('Itemid') == 16) echo 'class="activeNav"';?>>
                <span class="nav-upper">BESTIL</span>
                <span class="nav-lower">Se vores udvalg af varer</span>
            </a>
        </li>
        <li>
            <a href="index.php?option=com_ecommerce&view=orders&Itemid=17" <?php if(JRequest::getVar('Itemid') == 17) echo 'class="activeNav"';?>>
                <span class="nav-upper">MINE BESTILINGER</span>
                <span class="nav-lower">Hvad har jeg tidligere bestilt</span>
            </a>
        </li>
        <li>
            <a href="index.php?option=com_ecommerce&view=overall&Itemid=18" <?php if(JRequest::getVar('Itemid') == 18) echo 'class="activeNav"';?>>
                <span class="nav-upper">OVERSIGT</span>
                <span class="nav-lower">Betalings- og vareoversigt</span>
            </a>
        </li>
        <li>
            <a href="index.php?option=com_user&view=user&layout=form&Itemid=19" <?php if(JRequest::getVar('Itemid') == 19) echo 'class="activeNav"';?>>
                <span class="nav-upper">PROFIL</span>
                <span class="nav-lower">Ret mine oplysninger</span>
            </a>
        </li>				
    </ul>
    <div id="nav-shopcart" class="clear-fix">
        <div id="nav-sc-l">
            <p>Antal varer: <?php if($session->get('itemAmount')) echo $session->get('itemAmount'); else echo 0;?></p>
            <p>Total beløb: <?php echo number_format($moneyToTal,0,',','.');?> DKK</p>
        </div>
        <a id="nav-sc-r">GÅ TIL KURV</a>
    </div>
</div>
<!--Navigation end-->
<div class="shopcart-list">
    <form action="">
    <div id="scrollbar1">
        <div class="scrollbar"><div class="track"><div class="thumb"><div class="end"></div></div></div></div>
        <div class="viewport">
            <div class="overview" id="overview">                        
               <?php for ($i=1; $i<=$session->get('itemAmount'); $i++) { ?>
					<div class="eachCart">                        		
                        <p class="eachCart-name"><?php echo $session->get('name'.$i);?></p>
                        <input type="hidden" value="<?php echo $session->get('id'.$i);?>" />
                        <a class="eachClose" href="javascript:void(0);"></a>
                        <div class="eachCart-box clear-fix">
                            <p class="eachCart-value">Pris: <?php echo $session->get('price'.$i);?> kr</p>
                            <div class="eachCart-quantity clear-fix">
                                <a class="btnDown">Down</a>
                                <input class="inputSoluong" type="text" value="<?php echo $session->get('quantity'.$i);?>" />
                                <a class="btnUp">Up</a>
                                <input type="hidden" value="<?php echo $session->get('id'.$i);?>" />
                                <a class="btnRefresh" href="javascript:void(0);">Refresh</a>
                            </div>
                        </div>                        		
                    </div>
				<?php }?>
            </div>
        </div>
    </div><!-- end div scrollbar -->
    <div class="shopcart-setting clear-fix">
        <textarea rows="" cols="" placeholder="Meddelse til vores varepakning..." id="comment"></textarea>
        <a id="btnShopcart-close">Close</a>
        <a id="btnCheckout" href="index.php?option=com_ecommerce&view=overall&layout=confirm&Itemid=18">Checkout</a>
    </div>
    </form>
</div>